<template>
  <CustomDialog v-model:visible="showDialog" width="520px">
    <div class="dialog-content">
      <div class="title">预订开台</div>
      <div class="desktop-number">
        <span>桌台</span>
        <div>{{ desk?.name || desk?.recordInfo?.desk_name || '-' }}</div>
      </div>
      <div class="dialog-input-box">
        <div class="desc">人数</div>
        <div class="dialog-input">
          <el-input
            v-model.trim="useNumStr"
            type="number"
            class="input"
            placeholder="请输入人数"
            :disabled="loading"
            @keydown.enter.prevent="onConfirm"
          />
          <img
            class="image"
            src="@/assets/images/home/input-clear.png"
            alt=""
            @click="handleClearInput"
            v-if="useNumStr"
          />
        </div>
      </div>
      <NumberKeyboard
        :isFirstZero="false"
        :showRadixPoint="false"
        v-model:keyword="useNumStr"
        :loading="loading"
        @onInputKeyboard="onInputKeyboard"
        @onCancel="handleClearInput"
        @onConfirm="onConfirm"
      />
    </div>
  </CustomDialog>
</template>

<script setup lang="ts">
import { ref, watch, computed } from 'vue'
import { ElMessage } from 'element-plus'
import CustomDialog from '@/components/CustomDialog/Index.vue'
import NumberKeyboard from '@/components/NumberKeyboard/Index.vue'
import { openSingleDesk } from '@/api/desktop'

const props = defineProps<{
  modelValue: boolean
  desk: any | null
}>()

const emit = defineEmits(['update:modelValue', 'success'])

const showDialog = ref(false)
const loading = ref(false)
const useNum = ref<number | null>(null)
const useNumStr = computed({
  get() {
    return useNum.value !== null ? String(useNum.value) : ''
  },
  set(val: string) {
    const n = Number(val)
    useNum.value = isNaN(n) ? null : n
  }
})

watch(
  () => props.modelValue,
  val => {
    showDialog.value = val
    if (val) useNum.value = null
  },
  { immediate: true }
)
watch(showDialog, val => emit('update:modelValue', val))

const handleClearInput = () => (useNum.value = null)
const onInputKeyboard = (val: string) => {
  const n = Number(val)
  useNum.value = isNaN(n) ? null : n
}

const onConfirm = async () => {
  const capacity = props.desk?.capacity ?? props.desk?.desk?.capacity
  const deskId = props.desk?.id ?? props.desk?.desk_id ?? props.desk?.desk?.id
  const num = Number(useNum.value)
  if (!num || num <= 0) return ElMessage.warning('请输入用餐人数')
  if (capacity && num > capacity) return ElMessage.warning('用餐人数不能大于桌台可用人数')
  if (!deskId) return ElMessage.error('无效的桌台信息')

  try {
    loading.value = true
    const res = await openSingleDesk({ use_num: num, desk_id: deskId })
    if (res?.status === 200) {
      ElMessage.success(res.data.message || '开台成功')
      showDialog.value = false
      emit('success')
    }
  } catch (e) {
    ElMessage.error('开台失败')
  } finally {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
.dialog-content {
  .title {
    font-size: 20px;
    font-family:
      PingFang SC,
      PingFang SC-Bold;
    font-weight: 700;
    text-align: center;
    color: #333333;
  }
  .desktop-number {
    margin-top: 30px;
    display: flex;
    align-items: center;
    padding-left: 22px;
    > span {
      font-size: 16px;
      font-family:
        PingFang SC,
        PingFang SC-Regular;
      font-weight: 400;
      color: #333333;
      margin-right: 20px;
    }
    > div {
      font-size: 14px;
      font-family:
        PingFang SC,
        PingFang SC-Bold;
      font-weight: 700;
      color: #333333;
    }
  }
  .dialog-input-box {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
    margin-top: 30px;
    padding-left: 22px;
    .desc {
      font-size: 16px;
      font-family:
        PingFang SC,
        PingFang SC-Regular;
      font-weight: 400;
      color: #333333;
      margin-right: 20px;
    }
    .dialog-input {
      width: 420px;
      height: 48px;
      background: #ffffff;
      border: 1px solid #d2d2d2;
      border-radius: 12px;
      box-sizing: border-box;
      padding: 0 20px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .input {
        :deep(.el-input__wrapper) {
          box-shadow: none;
          padding: 0;
        }
        width: 400px;
      }
      .image {
        flex-shrink: 0;
        width: 24px;
        height: 24px;
        cursor: pointer;
      }
    }
  }
}
</style>
